package com.xiaoxiong.mami.dao.mapperImpl;

import com.xiaoxiong.mami.dao.daomain.UserToken;
import com.xiaoxiong.mami.dao.mapper.UserTokenMapper;
import org.apache.ibatis.annotations.*;

/**
 * Created by Administrator on 2017/8/8.
 */
@Mapper
public interface UserTokenMapperImpl extends UserTokenMapper {

    @Select("select * from user_token where token=#{token}")
    public UserToken queryTokenByToken(String token);

    @Select("select * from user_token where refreshToken=#{refreshToken}")
    public UserToken queryTokenByRefreshToken(String refreshToken);

    @Select("select * from user_token where userId=#{userId} order by updateTime desc limit 1")
    public UserToken queryTokenByUserId(String userId);

    @Select("select count(*) from user_token where userId=#{userId} ")
    public int queryTokenCount(String userId);

    @Select("delete from user_token where userId=#{userId} ")
    public int deleteToken(String userId);

    @Select("select * from user_token where token=#{token} or refreshToken=#{token}")
    public UserToken queryTokenByTokenAndRefreshToken(String token);

    @Insert({
            "insert into user_token (token, ",
            "userId, refreshToken, ",
            "expireTime, updateTime, ",
            "createTime)",
            "values ( #{token,jdbcType=VARCHAR}, ",
            "#{userId,jdbcType=VARCHAR}, #{refreshToken,jdbcType=VARCHAR}, ",
            "604800, now(), ",
            "now())"
    })
    public int insertUserToken(UserToken userToken);


    @Update({
            "update user_token set token = #{token}, updateTime = now() where userId = #{userId}"
    })
    public int updateUserToken(@Param("userId") String userId, @Param("token")String token);


}
